<?php

namespace app\api\controller\waimai;

use app\common\controller\ApiController;
use app\api\service\DeliveryService;
use think\facade\Request;
use think\exception\ValidateException;

// 店铺相关操作
class Shop extends ApiController
{

    //新增店铺/修改店铺---全平台
    public function storeOperationUnify(){
       $params = Request::post();

       try {
            validate([
                'storeName'  => 'require', 
                'cityName'  => 'require', 
                'address'  => 'require', 
                'addressDetail'  => 'require', 
                'latitude'  => 'require', 
                'longitude'  => 'require', 
                'phone'  => 'require', 
                'goodType'  => 'require', 
                'operationType'  => 'require', 
                'platformStoreId'  => 'require', 
            ])->message([
                'storeName.require'  => 'storeName必须传递',           
                'cityName.require'  => 'cityName必须传递',           
                'address.require'  => 'address必须传递',           
                'addressDetail.require'  => 'addressDetail必须传递',           
                'latitude.require'  => 'latitude必须传递',           
                'longitude.require'  => 'longitude必须传递',           
                'phone.require'  => 'phone必须传递',           
                'goodType.require'  => 'goodType必须传递',           
                'operationType.require'  => 'operationType必须传递',           
                'platformStoreId.require'  => 'platformStoreId必须传递',           
            ])->check($params);
        } catch (ValidateException $e) {
            output_error($e->getError());
        }

        try {
            if(strpos($params['app_type'],'dev')){//测试
               $app_type_arr= get_pro_dev_arr(1);
            }else{//正式
               $app_type_arr= get_pro_dev_arr(2);
            }
           
            $data=[];
            foreach($app_type_arr as $k => $v){
                $params['app_type'] =$v;
                $service = new DeliveryService($v);
                $result = $service->storeOperation($params);

                if(isset($result['data']['storeId'])){
                  $data[$k]['app_type']=$v;
                  $data[$k]['app_type_str']=get_app_type_zw($v);
                  $data[$k]['storeId']=$result['data']['storeId'];
                }
            }

        } catch (\Exception $e) {
             output_error($result["msg"]??'请求失败');
        }

        if($data){
            output_success('success', $data);
        }else{
            output_error($result["msg"]??'请求失败');
        }
    }

    //新增店铺/修改店铺----单一平台
    public function storeOperation(){
       $params = Request::post();

       try {
            validate([
                'storeName'  => 'require', 
                'cityName'  => 'require', 
                'address'  => 'require', 
                'addressDetail'  => 'require', 
                'latitude'  => 'require', 
                'longitude'  => 'require', 
                'phone'  => 'require', 
                'goodType'  => 'require', 
                'operationType'  => 'require', 
                'platformStoreId'  => 'require', 
            ])->message([
                'storeName.require'  => 'storeName必须传递',           
                'cityName.require'  => 'cityName必须传递',           
                'address.require'  => 'address必须传递',           
                'addressDetail.require'  => 'addressDetail必须传递',           
                'latitude.require'  => 'latitude必须传递',           
                'longitude.require'  => 'longitude必须传递',           
                'phone.require'  => 'phone必须传递',           
                'goodType.require'  => 'goodType必须传递',           
                'operationType.require'  => 'operationType必须传递',           
                'platformStoreId.require'  => 'platformStoreId必须传递',           
            ])->check($params);
        } catch (ValidateException $e) {
            output_error($e->getError());
        }

        try {
            $service = new DeliveryService($this->app_type);
            $result = $service->storeOperation($params);
            
        } catch (\Exception $e) {
             output_error($result["msg"]??'请求失败');
        }

        if($result["success"]){
            output_success('success', $result['data']);
        }else{
            output_error($result["msg"]??'请求失败');
        }
    }

    //批量新增店铺
    public function addStores(){

        $params = Request::post();

        try {
            $service = new DeliveryService($this->app_type);
            $result = $service->addStores($params);
            
        } catch (\Exception $e) {
             output_error($result["msg"]??$e->getMessage());
             output_error($result["msg"]??'请求失败');
        }

        if($result["success"]){
            output_success('success', $result['data']);
        }else{
            output_error($result["msg"]??'请求失败');
        }
    }

    // 分页查询商户店铺
    public function queryAllStores()
    {
        $params = Request::post();

        // 处理逻辑
        $service = new DeliveryService($this->app_type);
        $result = $service->queryAllStores($params);

        if($result["success"]){
            output_success('success', $result['data']);
        }else{
            output_error($result["msg"]??'请求失败');
        }

    }
}